package v1

import (
	"errors"
	"gin-study/app/http/requests"
	"gin-study/app/http/responses"
	"gin-study/app/services"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

var Order = new(order)

type order struct {
	controller
}

func (this *order) Index(c *gin.Context) interface{} {
	var req *requests.OrderListReq
	if err := c.Bind(&req); err != nil {
		return responses.BadRequest(err.Error(), nil)
	}
	orders := services.Order.GetOrderList(req.OrderSn, req.Status, req.UserId, this.GetPage(c))
	return responses.Ok("ok", gin.H{
		"orders": orders,
	})
}

func (this *order) Get(c *gin.Context) interface{} {
	id := c.Param("id")
	order, err := services.Order.GetOrderById(id)
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		return responses.NotFind("找不到这个订单", nil)
	}
	if err != nil {
		return responses.ServerError(err.Error(), nil)
	}
	return responses.Ok("ok", gin.H{
		"order": order,
	})
}

func (this *order) Post(c *gin.Context) interface{} {
	return responses.NotFind("404", nil)
}

func (this *order) Put(c *gin.Context) interface{} {
	return responses.NotFind("404", nil)
}

func (this *order) Delete(c *gin.Context) interface{} {
	return responses.NotFind("404", nil)
}
